package com.pz.cloud.framework.researchProj.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pz.cloud.framework.business.researchProj.vo.ResearchProjectVo;
import com.pz.cloud.framework.business.researchProj.vo.ResearchProjectModel;
import com.pz.cloud.framework.business.researchProj.entity.ResearchProject;
import com.pz.cloud.framework.business.researchProj.service.IResearchProjectService;
import com.pz.cloud.framework.business.workflow.annotation.AssembleDealFlag;
import com.pz.cloud.framework.business.workflow.entity.WaitDoneUser;
import com.pz.cloud.framework.business.workflow.enums.WorkflowEnum;
import com.pz.cloud.framework.business.workflow.model.ProcessRecordModel;
import com.pz.cloud.framework.business.workflow.service.IWaitDoneService;
import com.pz.cloud.framework.util.WorkflowUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.BaseController;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 科研项目
 * @Author: jeecg-boot
 * @Date:   2020-12-13
 * @Version: V1.0
 */
@Api(tags="科研项目")
@RestController
@RequestMapping("/researchProject")
@Slf4j
public class ResearchProjectController extends BaseController<ResearchProject, IResearchProjectService> {
	@Autowired
	private IResearchProjectService researchProjectService;
	@Autowired
	private IWaitDoneService waitDoneService;

	/**
	 * 分页列表查询
	 *
	 * @param researchProject
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "科研项目-分页列表查询")
	@ApiOperation(value="科研项目-分页列表查询", notes="科研项目-分页列表查询")
	@GetMapping(value = "/list")
	@AssembleDealFlag
	public Result<?> queryPageList(ResearchProject researchProject,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		researchProject.setActStatusNe(WorkflowEnum.ProcessStatus.流程结束.getStatus());
		IPage<ResearchProjectModel> pageList = researchProjectService.getModelPage(researchProject);
		if(pageList.getRecords()!=null){
			for (ResearchProjectModel record : pageList.getRecords()) {
				List<WaitDoneUser> waitDoneUser = waitDoneService.getCurUserByBizId(record.getId());
				if(!CollectionUtils.isEmpty(waitDoneUser)){
					String waitUserName = waitDoneUser.stream().map(WaitDoneUser::getWaitUserName).collect(Collectors.joining(","));
					record.setWaitHandler(waitUserName);
				}
			}
		}
		return Result.OK(pageList);
	}

	 /**
	  * 分页列表查询我的列表
	  *
	  * @param researchProject
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "科研项目-分页我的列表查询")
	 @ApiOperation(value="科研项目-分页我的列表查询", notes="科研项目-分页我的列表查询")
	 @GetMapping(value = "/myList")
	 public Result<?> queryMyList(ResearchProject researchProject,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 IPage<ResearchProjectModel> pageList = researchProjectService.getModelPage(researchProject);
		 if(pageList.getRecords()!=null){
			 for (ResearchProjectModel record : pageList.getRecords()) {
				 List<WaitDoneUser> waitDoneUser = waitDoneService.getCurUserByBizId(record.getId());
				 if(!CollectionUtils.isEmpty(waitDoneUser)){
					 String waitUserName = waitDoneUser.stream().map(WaitDoneUser::getWaitUserName).collect(Collectors.joining(","));
					 record.setWaitHandler(waitUserName);
				 }
			 }
		 }
		 return Result.OK(pageList);
	 }

	/**
	 *   添加
	 *
	 * @param researchProject
	 * @return
	 */
	@AutoLog(value = "科研项目-添加")
	@ApiOperation(value="科研项目-添加", notes="科研项目-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ResearchProjectVo researchProject) {
		researchProjectService.add(researchProject);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param researchProject
	 * @return
	 */
	@AutoLog(value = "科研项目-编辑")
	@ApiOperation(value="科研项目-编辑", notes="科研项目-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ResearchProjectVo researchProject) {
		researchProjectService.edit(researchProject);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "科研项目-通过id删除")
	@ApiOperation(value="科研项目-通过id删除", notes="科研项目-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		researchProjectService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "科研项目-批量删除")
	@ApiOperation(value="科研项目-批量删除", notes="科研项目-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.researchProjectService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "科研项目-通过id查询")
	@ApiOperation(value="科研项目-通过id查询", notes="科研项目-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ResearchProjectVo vo = researchProjectService.getVoById(id);
		List<ProcessRecordModel> record = waitDoneService.findSignleRecordByBizId(id);
		vo.setApproveRecords(record);
		return Result.OK(vo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param researchProject
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ResearchProject researchProject) {
        return super.exportXls(request, researchProject, ResearchProject.class, "科研项目");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ResearchProject.class);
    }



}
